package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Queue;

@Service
@SuppressWarnings("all")
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitService rabbitService;

    /**
     * 记录支付信息
     * @param orderInfo
     * @param name
     */
    @Override
    public void savePayment(OrderInfo orderInfo, String name) {

        //查询是否已经存在了支付记录
        LambdaQueryWrapper<PaymentInfo> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOrderId,orderInfo.getId());
        queryWrapper.eq(PaymentInfo::getPaymentType,PaymentType.ALIPAY.name());

        Integer count = paymentInfoMapper.selectCount(queryWrapper);
        //判断
        if(count>0){

            return ;
        }


        //创建记录对象
        PaymentInfo paymentInfo=new PaymentInfo();
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setUserId(orderInfo.getUserId());
        paymentInfo.setPaymentType(PaymentType.ALIPAY.name());
        paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
        paymentInfo.setSubject(orderInfo.getTradeBody());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());


        //新增
        paymentInfoMapper.insert(paymentInfo);

    }

    /**
     * 获取记账信息
     * @param out_trade_no
     * @param name
     * @return
     */
    @Override
    public PaymentInfo getPaymentInfo(String out_trade_no, String name) {

        QueryWrapper<PaymentInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("out_trade_no",out_trade_no);
        queryWrapper.eq("payment_type",name);
        return paymentInfoMapper.selectOne(queryWrapper);
    }

    /**
     * 修改记账成功信息状态
     * @param out_trade_no
     * @param name
     */
    @Override
    public void updatePaymentStatus(String out_trade_no, String name, Map<String ,String> paramsMap) {

        try {
            //查询支付信息对象
            PaymentInfo payment = getPaymentInfo(out_trade_no, name);
            //判断
            if(payment==null) {
                return;
            }


            //设置修改内容
            PaymentInfo paymentInfo=new PaymentInfo();
            paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
            paymentInfo.setTradeNo(paramsMap.get("trade_no"));
            paymentInfo.setCallbackTime(new Date());
            paymentInfo.setCallbackContent(JSON.toJSONString(paramsMap));

            //设置条件
            QueryWrapper<PaymentInfo> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("out_trade_no",out_trade_no);
            queryWrapper.eq("payment_type",name);


            paymentInfoMapper.update(paymentInfo,queryWrapper);


            //发送消息通知订单
            rabbitService.sendMessaged(
                    MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,
                    MqConst.ROUTING_PAYMENT_PAY,
                    payment.getOrderId()
            );



        } catch (Exception e) {
            //删除标识
            redisTemplate.delete(paramsMap.get("notify_id"));
            e.printStackTrace();
        }





    }

    /**
     * 关闭支付记录
     * @param orderId
     */
    @Override
    public void closePayment(Long orderId) {

        //封装条件
        QueryWrapper<PaymentInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId);
        //获取支付记录
        Integer count = paymentInfoMapper.selectCount(queryWrapper);
        if(count==0) {
            return;
        }

        //关闭支付信息
        PaymentInfo paymentInfo=new PaymentInfo();
        paymentInfo.setPaymentStatus(PaymentStatus.CLOSED.name());

        paymentInfoMapper.update(paymentInfo,queryWrapper);


    }
}
